package com.chx.service.Impl;

import com.chx.constant.StatusConstant;
import com.chx.dto.*;
import com.chx.entity.Products;
import com.chx.entity.StockLogs;
import com.chx.entity.StockOutOrderDetails;
import com.chx.entity.StockOutOrders;
import com.chx.mapper.*;
import com.chx.result.PageResult;
import com.chx.service.StockOutOrderService;
import com.chx.vo.StockInOrderDetailsVo;
import com.chx.vo.StockInOrderVo;
import com.chx.vo.StockOutOrderDetailsVo;
import com.chx.vo.StockOutOrderVo;
import com.chx.wrapper.StockOutOrderWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/31
 */
@Service
public class StockOutOrderServiceImpl implements StockOutOrderService {
    @Autowired
    private StockOutOrderMapper stockOutOrderMapper;
    @Autowired
    private StockOutOrderDetailMapper stockOutOrderDetailMapper;
    @Autowired
    private StockLogMapper stockLogMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public void save(StockOutOrderDto stockOutOrderDto) {
        StockOutOrders stockOutOrders = new StockOutOrders();
        BeanUtils.copyProperties(stockOutOrderDto, stockOutOrders);
        //1、订单类型为其他入库
        stockOutOrders.setRelatedOrderType(StatusConstant.ENABLE);
        //2、总金额
        Double  totalAmount = 0.0;
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList())
            totalAmount += stockOutOrderDetailsDto.getSubtotal();
        stockOutOrders.setTotalAmount(totalAmount);
        stockOutOrderMapper.insert(stockOutOrders);

        StockOutOrderDetails stockOutOrderDetails = new StockOutOrderDetails();
        stockOutOrderDetails.setOrderId(stockOutOrders.getOrderId());
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockOutOrderDetailsDto, stockOutOrderDetails);
            stockOutOrderDetailMapper.insert(stockOutOrderDetails);
        }
    }

    @Transactional
    @Override
    public void update(StockOutOrderDto stockOutOrderDto) {
        StockOutOrders stockOutOrders = new StockOutOrders();
        BeanUtils.copyProperties(stockOutOrderDto, stockOutOrders);
        Double  totalAmount = 0.0;
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList())
            totalAmount += stockOutOrderDetailsDto.getSubtotal();
        stockOutOrders.setTotalAmount(totalAmount);
        stockOutOrderMapper.update(stockOutOrders);

        stockOutOrderDetailMapper.delete(stockOutOrderDto.getOrderId());
        StockOutOrderDetails  stockOutOrderDetails = new StockOutOrderDetails();
        stockOutOrderDetails.setOrderId(stockOutOrders.getOrderId());
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockOutOrderDetailsDto, stockOutOrderDetails);
            stockOutOrderDetailMapper.insert(stockOutOrderDetails);
        }
    }

    @Override
    public void updateStatus(Long id) {
        StockOutOrders stockOutorders = new StockOutOrders();
        stockOutorders.setOrderId(id);
        stockOutorders.setStatus(StatusConstant.ENABLE);
        stockOutOrderMapper.update(stockOutorders);
    }

    @Override
    public StockOutOrderVo getById(Long id) {
        StockOutOrders stockOutOrders = stockOutOrderMapper.getById(id);
        List<StockOutOrderDetails> stockOutOrderDetails = stockOutOrderDetailMapper.getByOrderId(id);
        StockOutOrderVo stockOutOrderVo = new StockOutOrderVo();
        BeanUtils.copyProperties(stockOutOrders, stockOutOrderVo);
        stockOutOrderVo.setUserName(userMapper.getById(stockOutOrders.getUserId()).getRealName());
        List<StockOutOrderDetailsVo>  stockOutOrderDetailsVos = new ArrayList<>();
        for(StockOutOrderDetails stockOutOrderDetail:stockOutOrderDetails){
            StockOutOrderDetailsVo stockOutOrderDetailsVo = new StockOutOrderDetailsVo();
            BeanUtils.copyProperties(stockOutOrderDetail, stockOutOrderDetailsVo);
            stockOutOrderDetailsVo.setProductName(productMapper.getById(stockOutOrderDetail.getProductId()).getProductName());
            stockOutOrderDetailsVos.add(stockOutOrderDetailsVo);
        }
        stockOutOrderVo.setStockOutOrderDetailsVoList(stockOutOrderDetailsVos);
        return stockOutOrderVo;
    }

    @Override
    public PageResult page(StockOutOrderPageQueryDto stockOutOrderPageQueryDto) {
        PageHelper.startPage(stockOutOrderPageQueryDto.getPage(), stockOutOrderPageQueryDto.getPageSize());
        Page<StockOutOrders> page = stockOutOrderMapper.page(stockOutOrderPageQueryDto);
        long total = page.getTotal();
        List<StockOutOrders> records = page.getResult();
        List<StockOutOrderWrapper> stockOutOrderWrappers = records.stream().map(record ->{
            String userName = userMapper.getById(record.getUserId()).getUsername();
            return  new StockOutOrderWrapper(record, userName);
        }).collect(Collectors.toList());
        return new PageResult(total, stockOutOrderWrappers);
    }

    @Transactional
    @Override
    public void commit(Long id) {
        StockOutOrders stockOutorders = new StockOutOrders();
        stockOutorders.setOrderId(id);
        stockOutorders.setStatus(StatusConstant.ROLE_USER);
        stockOutOrderMapper.update(stockOutorders);
        // 日志表插入,还会更改商品的库存数目
        Products products = new Products();
        StockOutOrderVo stockOutOrderVo = getById(id);

        StockLogs stockLogs = new StockLogs();
        stockLogs.setChangeType(StatusConstant.ENABLE);
        if(Objects.equals(stockOutOrderVo.getRelatedOrderType(), StatusConstant.DISABLE))
            stockLogs.setRelatedOrderType(StatusConstant.ENABLE);
        else
            stockLogs.setRelatedOrderType(StatusConstant.ROLE_SUPER_ADMIN);
        stockLogs.setRelatedOrderId(id);
        stockLogs.setUserId(stockOutOrderVo.getUserId());
        stockLogs.setOperationTime(stockOutOrderVo.getOrderDate());
        stockLogs.setRemark(stockOutOrderVo.getRemark());
        for(StockOutOrderDetailsVo stockInOrderDetailsVo:stockOutOrderVo.getStockOutOrderDetailsVoList()){
            stockLogs.setProductId(stockInOrderDetailsVo.getProductId());
            stockLogs.setChangeQuantity(stockInOrderDetailsVo.getQuantity());
            stockLogs.setBeforeQuantity(productMapper.getById(stockInOrderDetailsVo.getProductId()).getStockQuantity());
            stockLogs.setAfterQuantity(stockLogs.getBeforeQuantity()-stockLogs.getChangeQuantity());

            products.setProductId(stockInOrderDetailsVo.getProductId());
            products.setStockQuantity(stockLogs.getAfterQuantity());
            productMapper.update(products);
            stockLogMapper.addStockLog(stockLogs);
        }
    }
}
